//获取url参数
function getQueryString(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
    var r = window.location.search.substr(1).match(reg);
    if (r != null)
        return unescape(r[2]);
    return null;
}

//编辑的时候获取数据填充表单
function showData() {
//    overlayer("open");
    $.ajax({
        type: "POST",
        async: false,
        url: "../../action/policy/get.do",
        data: {type: type, messageNo: id, instance: DepotName},
        dataType: "json",
        success: function(souData) {
            //souData = $.parseJSON(souData);
            var policy_desc = souData.Comment;
            var content = souData.Content
            $(".policy_desc").val(policy_desc);
            $("#Adv_WhiteHostListID").val(content.Adv_WhiteHostListID);
            $("#Trigger_HostListID").val(content.Trigger_HostList.HostListID.join("\n"));
            $("#Trigger_KWListID").val(content.Trigger_KWList.KWListID.join("\n"));
            $("#Adv_ID").val(content.Adv_ID);
            $("#AdvFrame_URL").val(content.AdvFrame_URL);
            $("#Adv_Token").val(content.Adv_Token);
            $("#Adv_Delay").val(content.Adv_Delay)
        }
    });


}
//获取表单数据
function generateParameter() {
    var obj = {
        "Adv_type": $("#Adv_type").val(),
        "Adv_WhiteHostListID": $("#Adv_WhiteHostListID").val(),
        "Trigger_HostList": {
            "HostListID": $("#Trigger_HostListID").val().split("\n")
        },
        "Trigger_KWList": {
            "KWListID": $("#Trigger_KWListID").val().split("\n")
        },
        "Adv_ID": $("#Adv_ID").val(),
        "AdvFrame_URL": $("#AdvFrame_URL").val(),
        "Adv_Token": $("#Adv_Token").val(),
        "Adv_Delay": $("#Adv_Delay").val(),
        "MessageSerialNo": "0"
    }
    return obj;
}

//添加数据
function save() {
    var form = $(".form-horizontal").validate();
    if (!form.form()) {
        return;
    }
    var comment = $(".policy_desc").val();
    var policy = JSON.stringify(generateParameter());
    $.ajax({
        type: "POST",
        async: false,
        url: "../../action/policy/create.do",
        data: {comment: comment, policy: policy, type: type, instance: DepotName},
        dataType: "json",
        success: function(data) {
            if (data.result == "true") {
                alert("添加成功！");
                previous();
            } else {
                alert("添加失败!")
            }
        }
    });
}
//编辑数据
function edit() {
    var form = $(".form-horizontal").validate();
    if (!form.form()) {
        return;
    }
    var comment = $(".policy_desc").val();
    var policy = JSON.stringify(generateParameter());
    $.ajax({
        type: "POST",
        async: false,
        url: "../../action/policy/update.do",
        data: {messageNo: id, comment: comment, policy: policy, type: type, instance: DepotName},
        dataType: "json",
        success: function(data) {
            if (data.result == "true") {
                alert("编辑成功！");
                previous();
            } else {
                alert("编辑失败!")
            }
        }
    });
}
//返回上一级
function previous() {
    window.location = "../policyList.html?type=" + type + "&id=" + DepotID + "&name=" + base64.encode(DepotName) + "";
}
/*
 * 查看旧版本的策略内容
 */
function show_oldPolicy(array, number) {
    var souData = array[number];
    $(".header").text("当前策略在" + souData.CreateTime + "创建,ID为" + souData.MessageNo + ",版本号为" + souData.MessageSerialNo + ",属于" + souData.Instance + "策略库");

    var policy_desc = souData.Comment;
    var content = souData.Content
    $(".policy_desc").val(policy_desc);
    $("#Adv_WhiteHostListID").val(content.Adv_WhiteHostListID);
    $("#Trigger_HostListID").val(content.Trigger_HostList.HostListID.join("\n"));
    $("#Trigger_KWListID").val(content.Trigger_KWList.KWListID.join("\n"));
    $("#Adv_ID").val(content.Adv_ID);
    $("#AdvFrame_URL").val(content.AdvFrame_URL);
    $("#Adv_Token").val(content.Adv_Token);
    $("#Adv_Delay").val(content.Adv_Delay)
}

/*
 *获取所有版本的策略内容 
 */
function getAllPolicy() {
    $.ajax({
        type: "GET",
        async: false,
        url: "../../action/policy/history_policy_info.do",
        data: {type: type, instance: getQueryString("DepotName"), messageNo: id},
        dataType: "json",
        success: function(data) {
            policyArray = data;
            policyAmount = data.length
        }
    });
}